package com.ruoyi.wechat.open.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wechat.open.service.WxOpenServiceDemo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpDataCubeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenCreateResult;
import me.chanjar.weixin.open.bean.WxOpenGetResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxopenaccount")
public class WxOpenAccountController {
    private WxOpenServiceDemo wxOpenServiceDemo;
    /**
     * 创建开放平台账号
     * @param appId
     * @return
     */
    @GetMapping("/create")
//	@PreAuthorize("@ss.hasPermi('wxmp:wxsummary:index')")
    public AjaxResult create(String appId) {

       WxOpenComponentService componentService= wxOpenServiceDemo.getWxOpenComponentService();

        try {
            WxOpenCreateResult wxOpenCreateResult = componentService.createOpenAccount(appId, WxConsts.AppIdType.MP_TYPE);

            return AjaxResult.success(wxOpenCreateResult);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return AjaxResult.error();
    }
    /**
     * 绑定开放平台账号
     * @param appId
     * @return
     */
    @GetMapping("/bind")
//	@PreAuthorize("@ss.hasPermi('wxmp:wxsummary:index')")
    public AjaxResult bind(String appId) {

        WxOpenComponentService componentService= wxOpenServiceDemo.getWxOpenComponentService();

        String openAppid = "wx5f6dbc7fb8aa151a";
        try {
            Boolean wxOpenCreateResult = componentService.bindOpenAccount(appId, WxConsts.AppIdType.MP_TYPE,openAppid);

            return AjaxResult.success(wxOpenCreateResult);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return AjaxResult.error();
    }

    /**
     * 创建开放平台账号
     * @param appId
     * @return
     */
    @GetMapping("/unbind")
//	@PreAuthorize("@ss.hasPermi('wxmp:wxsummary:index')")
    public AjaxResult unbind(String appId) {

        WxOpenComponentService componentService= wxOpenServiceDemo.getWxOpenComponentService();
        String openAppid = "wx5f6dbc7fb8aa151a";
        try {
            Boolean wxOpenCreateResult = componentService.unbindOpenAccount(appId, WxConsts.AppIdType.MP_TYPE,openAppid);

            return AjaxResult.success(wxOpenCreateResult);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return AjaxResult.error();
    }
    /**
     * 获取开放平台账号
     * @param appId
     * @return
     */
    @GetMapping("/get")
//	@PreAuthorize("@ss.hasPermi('wxmp:wxsummary:index')")
    public AjaxResult get(String appId) {
        WxOpenComponentService componentService= wxOpenServiceDemo.getWxOpenComponentService();


//
//        appId = "wx40e1ebf8ab4a7086";
//
//        String refreshToken = "refreshtoken@@@HE_zh3Cj9Vk1sHsznNFwRURfFgDFaPkThV8DcplxV4M";
//
//
//        //start 用于取出 RefreshToken(本地测试使用)
//        WxOpenConfigStorage wxOpenConfigStorage = wxOpenServiceDemo.getWxOpenComponentService().getWxOpenConfigStorage();
//
//        wxOpenConfigStorage.setAuthorizerRefreshToken(appId,refreshToken);

//        //end
//        try {
//            WxOpenGetResult wxOpenGetResult =   componentService.getOpenAccount(appId,WxConsts.AppIdType.MP_TYPE);
//
//            return AjaxResult.success(wxOpenGetResult);
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//        }

        return AjaxResult.error();
    }

}
